//
//  XLLoginRegisterView.m
//  industry
//
//  Created by xl on 2020/11/13.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLLoginRegisterView.h"
#import "XLLoginRegisterViewProtocol.h"
#import "XLLoginView.h"
#import "XLRegisterView.h"
#import "XLRegisterFinishView.h"
#import "XLForgetPSView.h"
#import "YQBindView.h"
#import "XLForgetPSFinishView.h"
#import "XLLoginRegisterVC.h"
#import "XLUserDefaults.h"
#import "XLLoginRegisterPresenterProtocol.h"

@interface XLLoginRegisterView ()<XLLoginRegisterViewProtocol>

@property (nonatomic, strong) XLImageView *bgIG;
@property (nonatomic, strong) XLScrollView *scrollView;
@property (nonatomic, strong) XLImageView *logoIG;
@property (nonatomic, strong) XLLabel *titleLB;

@property (nonatomic, assign) XLEntryLoginRegisterVCType currentFromType;
@property (nonatomic, strong) AUToast *toastLoad;

@property (nonatomic, strong) XLLoginView *loginView;
@property (nonatomic, strong) XLRegisterView *registerView;
@property (nonatomic, strong) XLRegisterFinishView *rgFinishView;
@property (nonatomic, strong) XLForgetPSView *forgetView;
@property (nonatomic, strong) YQBindView *bindView;
@property (nonatomic, strong) XLForgetPSFinishView *forgetFinishView;

@end

@implementation XLLoginRegisterView

- (void)dealloc {
    XLLog(@"XLLoginRegisterView dealloc");
}

#pragma mark - XLLoginRegisterViewProtocol
- (void)buildViewFromType:(XLEntryLoginRegisterVCType)type {
    [self addSubview:self.bgIG];
    [self addSubview:self.scrollView];
    [_bgIG addSubview:self.logoIG];
    [_bgIG addSubview:self.titleLB];
    
    [self makeContraints];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    if (!app_Name) app_Name = [infoDictionary valueForKey:@"CFBundleName"];
    if (!app_Name) app_Name = [infoDictionary valueForKey:@"CFBundleExecutable"];
    _titleLB.text = (type == XLEntryLoginRegisterVCTypeLogin) ? [NSString stringWithFormat:@"您好，\n欢迎使用%@", app_Name]:@"您好，\n请注册您的账号";
    self.currentFromType = type;
    if (type == XLEntryLoginRegisterVCTypeLogin) {
        _titleLB.text = [NSString stringWithFormat:@"您好，\n欢迎使用%@", app_Name];
        [self.scrollView addSubview:self.loginView];
        
        //请求是否显示员工登录选中按钮
        XLSend(self.baseController.presenter, XLLoginRegisterPresenterProtocol, requestShowStaffLoginStatus);
    } else if (type == XLEntryLoginRegisterVCTypeRegister) {
        _titleLB.text = @"您好，\n请注册您的账号";
        [self.scrollView addSubview:self.registerView];
        [_registerView refreshCheckImage];
    } else if (type == XLEntryLoginRegisterVCTypeRegisterFinish){
        _titleLB.text = @"您好，\n请注册您的账号";
        [self.scrollView addSubview:self.rgFinishView];
    } else if (type == XLEntryLoginRegisterVCTypeForgetPassword) {
        _titleLB.text = @"您好，\n请通过手机短信找回密码";
        [self.scrollView addSubview:self.forgetView];
        [_forgetView refreshCheckImage];
    } else if (type == XLEntryLoginRegisterVCTypeBind) {
        _titleLB.text = @"您好，\n请通过手机进行绑定";
        [self.scrollView addSubview:self.bindView];
        [_bindView refreshCheckImage];
    } else {
        _titleLB.text = @"您好，\n请设置您的新密码";
        [self.scrollView addSubview:self.forgetFinishView];
    }
}

//刷新获取校验码状态
- (void)refreshVerificationCodeButtonState:(BOOL)state {
    XLLoginRegisterVC *vc = (XLLoginRegisterVC *)self.baseController;
    if (vc.entryType == XLEntryLoginRegisterVCTypeLogin) {
        [_loginView refreshVerificationCodeButtonState:state];
    } else if (vc.entryType == XLEntryLoginRegisterVCTypeForgetPassword) {
        [_forgetView refreshVerificationCodeButtonState:state];
    } else if (vc.entryType == XLEntryLoginRegisterVCTypeBind) {
        [_bindView refreshVerificationCodeButtonState:state];
    } else if (vc.entryType == XLEntryLoginRegisterVCTypeRegister) {
        [_registerView refreshVerificationCodeButtonState:state];
    }
}

- (void)showLoadingAnimalWithTitle:(NSString *)title {
    self.toastLoad = [XLToast showLoadingTitle:title superView:self];
}

- (void)hideLoadingAnimal {
    if (self.toastLoad) {
        [self.toastLoad dismissToast];
    }
}

//刷新图片校验码
- (void)refreImageCodeCheck {
    XLLoginRegisterVC *vc = (XLLoginRegisterVC *)self.baseController;
    if (vc.entryType == XLEntryLoginRegisterVCTypeRegister) {//注册第一步校验界面
        [_registerView refreshCheckImage];
    } else if (vc.entryType == XLEntryLoginRegisterVCTypeForgetPassword) {//忘记密码校验第一步界面
        [_forgetView refreshCheckImage];
    } else if (vc.entryType == XLEntryLoginRegisterVCTypeBind) {//绑定第一步界面
        [_bindView refreshCheckImage];
    } else if (vc.entryType == XLEntryLoginRegisterVCTypeLogin) {//校验码登录校验
        [_loginView refreshCheckImage];
    }
}

//刷新是否可以显示员工登录按钮
- (void)refreshStaffLoginSelectBtnStatus:(BOOL)status isShow:(BOOL)isShow{
    XLLoginRegisterVC *vc = (XLLoginRegisterVC *)self.baseController;
    if (vc.entryType == XLEntryLoginRegisterVCTypeLogin) {
        [_loginView refreshStaffLoginSelectBtnStatus:status isShow:isShow];
    }
}

// 更新tip标签
- (void)updateTipWithBindType:(XLLoginBindType)type {
    [self.bindView updateTipWithBindType:type];
}

#pragma mark - Private
- (void)makeContraints {
    [_bgIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH * 306/375));
    }];
    
    [_logoIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgIG).offset(30);
        make.top.equalTo(self.bgIG).offset(STATUSBAR_HEIGHT + 35);
        make.size.mas_equalTo(CGSizeMake(150, 41));
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.logoIG);
        make.top.equalTo(self.logoIG.mas_bottom).offset(20);
    }];
    [_titleLB sizeToFit];
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.top.bottom.equalTo(self);
    }];
}

#pragma mark - setter/getter
- (XLScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [XLScrollView new];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT + 1);
    }
    return _scrollView;
}

- (XLImageView *)bgIG {
    if (!_bgIG) {
        _bgIG = [XLImageView new];
        _bgIG.image = XLIMAGE(@"loginbg");
    }
    return _bgIG;
}

- (XLImageView *)logoIG {
    if (!_logoIG) {
        _logoIG = [XLImageView new];
        _logoIG.image = XLIMAGE(@"logo_white");
    }
    return _logoIG;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:22];
        _titleLB.textColor = [UIColor whiteFontColor];
        _titleLB.numberOfLines = 2;
    }
    return _titleLB;
}

- (XLLoginView *)loginView {
    if (!_loginView) {
        _loginView = [[XLLoginView alloc] initWithFrame:CGRectMake(0, 179 + STATUSBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 179)];
    }
    return _loginView;
}

- (XLRegisterView *)registerView {
    if (!_registerView) {
        _registerView = [[XLRegisterView alloc] initWithFrame:CGRectMake(0, 179 + STATUSBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 179)];
    }
    return _registerView;
}

- (XLRegisterFinishView *)rgFinishView {
    if (!_rgFinishView) {
        _rgFinishView = [[XLRegisterFinishView alloc] initWithFrame:CGRectMake(0, 179 + STATUSBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 179)];
    }
    return _rgFinishView;
}

- (XLForgetPSView *)forgetView {
    if (!_forgetView) {
        _forgetView = [[XLForgetPSView alloc] initWithFrame:CGRectMake(0, 179 + STATUSBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 179)];
    }
    return _forgetView;
}

- (YQBindView *)bindView {
    if (!_bindView) {
        _bindView = [[YQBindView alloc] initWithFrame:CGRectMake(0, 179 + STATUSBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 179)];
    }
    return _bindView;
}

- (XLForgetPSFinishView *)forgetFinishView {
    if (!_forgetFinishView) {
        _forgetFinishView = [[XLForgetPSFinishView alloc] initWithFrame:CGRectMake(0, 179 + STATUSBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSBAR_HEIGHT - 179)];
    }
    return _forgetFinishView;
}

@end
